package com.andyliu.configer;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan(basePackages = "com.andyliu.sys.controller")
public class SwaggerConfig {
    @Bean
    public Docket api() {
        System.out.println("----andyliu-----正在启动swagger配置，当前类=SwaggerConfig.api()");
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
				/***
					重要的两个方法:
					apis():指定要生成文档的接口包基本路径
					paths():指定针对哪些请求生成接口文档
					参考官方资料：http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api
				****/
                //.apis(RequestHandlerSelectors.any())
              .apis(RequestHandlerSelectors.basePackage("com.andyliu"))
//                .paths(PathSelectors.ant("/sys/**"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }
    private List<ApiKey> securitySchemes() {
        return newArrayList(
                new ApiKey("Authorization", "andyliu", "header"));
    }
    private List<SecurityContext> securityContexts() {
        return newArrayList(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        // 所有包含"auth"的接口不需要使用securitySchemes
                        .forPaths(PathSelectors.regex("^(?!auth).*$"))
                        .build()
        );
    }
    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return newArrayList(
                new SecurityReference("Authorization", authorizationScopes));
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("信息管理平台项目接口文档")
                .description("信息管理平台API接口文档")
                .version("1.0.0")
                .termsOfServiceUrl("")
                .license("Andyliu")
                .licenseUrl("")
                .build();
    }
}